package eu.kuresson.tower.service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.crypto.keygen.KeyGenerators;

import eu.kuresson.tower.model.SecUser;

public class SecUserDetailsService implements UserDetailsService {
	private EntityManager entityManager;
    @PersistenceContext
    public void setEntityManager(EntityManager newEm){
        this.entityManager = newEm;
    }
    
	@Override
	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException, DataAccessException {
		
		UserDetails userDetails = entityManager.createQuery("from SecUser where userName = :userName", SecUser.class)
                .setParameter("userName", userName)
                .getSingleResult();

		return userDetails;
	}
}
